cd "C:\Users\tbohmelt\Dropbox\Coups Spatial\PSRM Paper\2_Final Version\PSRM Replication\"

* Note: set working directory according to your location of the files.

clear
pr drop _all
set more off
set matsize 8000                                                     

program define splag_ll

args lnf mu rho1 rho2 rho3 rho4 sigma
tempvar A rSL1 rSL2 rSL3 rSL4
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
gen double `rSL3'=`rho3'*SL3
gen double `rSL4'=`rho4'*SL4
scalar p1 = `rho1'
scalar p2 = `rho2'
scalar p3 = `rho3'
scalar p4 = `rho4'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix p3W3 = p3*W3
matrix p4W4 = p4*W4
matrix IpW = I_n - p1W1 - p2W2- p3W3- p4W4
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`rSL4'-`mu', 0, `sigma'))
end

spatwmat using "inverse_coup", name(W1)standardize

spatwmat using "Joint_Demo II.dta", name(W2)standardize

spatwmat using "inverse_coup", name(W3)standardize

spatwmat using "Joint_Demo II.dta", name(W4)standardize

drop _all
use "Monadic Baseline.dta", clear
global nobs = 3497
matrix I_n = I($nobs)
global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
global K couprisk1
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
mkmat $K, matrix(K)
matrix SL1 = W1*Z
svmat SL1, n(SL1)
matrix SL2 = W2*Z
svmat SL2, n(SL2)
matrix SL3 = W3*K
svmat SL3, n(SL3)
matrix SL4 = W4*K
svmat SL4, n(SL4)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (rho3:) (rho4:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init rho3:_cons=0
ml init rho4:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max

clear
discard
pr drop _all
set more off
set matsize 8000                                                        

program define splag_ll

args lnf mu rho1 rho2 rho3 rho4 sigma
tempvar A rSL1 rSL2 rSL3 rSL4
gen double `rSL1'=`rho1'*SL1
gen double `rSL2'=`rho2'*SL2
gen double `rSL3'=`rho3'*SL3
gen double `rSL4'=`rho4'*SL4
scalar p1 = `rho1'
scalar p2 = `rho2'
scalar p3 = `rho3'
scalar p4 = `rho4'
matrix p1W1 = p1*W1
matrix p2W2 = p2*W2
matrix p3W3 = p3*W3
matrix p4W4 = p4*W4
matrix IpW = I_n - p1W1 - p2W2- p3W3- p4W4
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`rSL4'-`mu', 0, `sigma'))
end

spatwmat using "inverse_coup_lost", name(W1)standardize

spatwmat using "Joint_Demo II.dta", name(W2)standardize

spatwmat using "inverse_coup_lost", name(W3)standardize

spatwmat using "Joint_Demo II.dta", name(W4)standardize

drop _all
use "Monadic Baseline.dta", clear
global nobs = 3497
matrix I_n = I($nobs)
global Y effective_number
global X polity2 durable ny_gdp_pcap_kd sp_pop_totl fuel_i war_total army_centrality lag_DV country_fe2-country_fe145 year_fe2- year_fe30
global Z lag_DV
global K couprisk1
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
mkmat $K, matrix(K)
matrix SL1 = W1*Z
svmat SL1, n(SL1)
matrix SL2 = W2*Z
svmat SL2, n(SL2)
matrix SL3 = W3*K
svmat SL3, n(SL3)
matrix SL4 = W4*K
svmat SL4, n(SL4)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (rho3:) (rho4:) (sigma:)
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init rho3:_cons=0
ml init rho4:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max
